<?php
namespace Admin\Driver;

    /*-------------------------------------------------
     * redis驱动类，需要安装phpredis扩展
     *
     * @athor   tang
     * @link    http://
     * @date    2017-06-25
     *-------------------------------------------------
    */

    class QRedis{
         
        private $_redis = null;
  
        public function __construct(){
            if($this->_redis === null){
                try{
                    $redis      = new \Redis();
                  //  dump($redis);
                    $hanedel    = $redis->connect('127.0.0.1',6379);
                    if($hanedel){
                        echo "服务器连接成功";
                        $this->_redis = $redis;
                    }else{
                        echo 'redis服务器无法链接';
                        $this->_redis = false;
                        exit;
                    }
                }catch(RedisException $e){
                    echo 'phpRedis扩展没有安装：' . $e->getMessage();
                    exit;
                }
            }
        }

        public function set($name, $value, $expire ="") {

            //对数组/对象数据进行缓存处理，保证数据完整性
            $value  =  (is_object($value) || is_array($value)) ? json_encode($value) : $value;
            if(is_int($expire) && $expire) {
                $result = $this->_redis->setex($name, $expire, $value);
            }else{
                $result = $this->_redis->set($name, $value);
            }
            return $result;
        }
        
        public function get($name) {

            $value = $this->_redis->get($name);
            $jsonData  = json_decode( $value, true );
            return ($jsonData === NULL) ? $value : $jsonData;   //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
        }
        /*
         * 队列尾追加
         *
         *
        */
        public function addRlist($key,$value){
            $result = $this->_redis->rPush($key,$value);
            return $result;
        }
        
        
        /*
         * 队列头追加
         *
         *
        */
        public function addLlist($key,$value){
            $result = $this->_redis->lPush($key,$value);
            return $result;
        }
        

        /*
         * 头出队列
         *
        */
        public function lpoplist($key){
            $result=$this->_redis->lPop($key);
            return $result;
        }
        

        /*
         * 尾出队列
         *
        */
        public function rpoplist($key){
            $result=$this->_redis->rPop($key);
            return $result;
        }
        

        
        /*
         * 查看队列
         *
        */
        public function showlist($key){
            $result = $this->_redis->lRange($key, 0, -1);
            return $result;
        }


        /**
         * 队列数量
         * 
        */
        public function listcount($key){
            $result = $this->_redis->lSize($key);
            return $result;
        }

        /*
         * 清空队列
         *
        */
        public function clearlist($key){
            $result = $this->_redis->delete($key);
            return $result;
        }



        /*
         * 获取redis资源对象
         *
        */
        public function getHandel(){
            return $this->_redis;
        }


        
    }



?>